package admin

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"outsourced-projects/pkg/utils"
	"outsourced-projects/service"
)

type Wxwork struct {
	service.Base
}

func (wxwork Wxwork) CheckinGetcheckinMonthdata(c *gin.Context) {
	var err error
	//数据获取
	jsondata := make(map[string]interface{})
	err = c.BindJSON(&jsondata)
	if err != nil {
		service.ReturnJson(c, 500, err.Error(), gin.H{})
		return
	}
	type ReqCheckinGetcheckinMonthdataStruct struct {
		Starttime  int      `json:"starttime"`
		Endtime    int      `json:"endtime"`
		Useridlist []string `json:"useridlist"`
	}

	//转换数据类型
	s := []string{}
	for _, val := range jsondata["userlist"].([]interface{}) {
		s = append(s, val.(string))
	}

	ReqCheckinGetcheckinMonthdata := &ReqCheckinGetcheckinMonthdataStruct{
		Starttime:  cast.ToInt(jsondata["starttime"]),
		Endtime:    cast.ToInt(jsondata["endtime"]),
		Useridlist: s,
	}
	b, err := json.Marshal(&ReqCheckinGetcheckinMonthdata)
	if err != nil {
		service.ReturnJson(c, 500, err.Error(), gin.H{})
		return
	}

	datas, err := utils.CheckinGetcheckinMonthdata(b)
	service.ReturnJson(c, 200, "获取成功", gin.H{
		"info": datas.Datas,
	})
}

func (wxwork Wxwork) CheckinGetcheckinDaydata(c *gin.Context) {
	var err error
	//数据获取
	jsondata := make(map[string]interface{})
	err = c.BindJSON(&jsondata)
	if err != nil {
		service.ReturnJson(c, 500, err.Error(), gin.H{})
		return
	}

	type ReqCheckinGetcheckinDaydataStruct struct {
		Starttime  int      `json:"starttime"`
		Endtime    int      `json:"endtime"`
		Useridlist []string `json:"useridlist"`
	}

	//转换数据类型
	s := []string{}
	for _, val := range jsondata["userlist"].([]interface{}) {
		s = append(s, val.(string))
	}

	ReqCheckinGetcheckinDaydata := &ReqCheckinGetcheckinDaydataStruct{
		Starttime:  cast.ToInt(jsondata["starttime"]),
		Endtime:    cast.ToInt(jsondata["endtime"]),
		Useridlist: s,
	}

	b, err := json.Marshal(&ReqCheckinGetcheckinDaydata)
	if err != nil {
		service.ReturnJson(c, 500, err.Error(), gin.H{})
		return
	}
	datas, err := utils.CheckinGetcheckinDaydata(b)

	service.ReturnJson(c, 200, "获取成功", gin.H{
		"info": datas.Datas,
	})
}

func (wxwork Wxwork) CheckinGetcheckindata(c *gin.Context) {
	var err error
	//数据获取
	jsondata := make(map[string]interface{})
	err = c.BindJSON(&jsondata)
	if err != nil {
		service.ReturnJson(c, 500, err.Error(), gin.H{})
		return
	}
	type ReqCheckinGetcheckindataStruct struct {
		Opencheckindatatype int      `json:"opencheckindatatype"`
		Starttime           int      `json:"starttime"`
		Endtime             int      `json:"endtime"`
		Useridlist          []string `json:"useridlist"`
	}

	//转换数据类型
	s := []string{}
	for _, val := range jsondata["userlist"].([]interface{}) {
		s = append(s, val.(string))
	}

	ReqCheckinGetcheckindata := ReqCheckinGetcheckindataStruct{
		Opencheckindatatype: cast.ToInt(jsondata["opencheckindatatype"]),
		Starttime:           cast.ToInt(jsondata["starttime"]),
		Endtime:             cast.ToInt(jsondata["endtime"]),
		Useridlist:          s,
	}

	b, err := json.Marshal(&ReqCheckinGetcheckindata)
	if err != nil {
		service.ReturnJson(c, 500, err.Error(), gin.H{})
		return
	}
	datas, err := utils.CheckinGetcheckindata(b)
	service.ReturnJson(c, 200, "获取成功", gin.H{
		"info": datas.Checkindata,
	})
}

func (wxwork Wxwork) CheckinGetcheckinschedulist(c *gin.Context) {
	var err error
	//数据获取
	jsondata := make(map[string]interface{})
	err = c.BindJSON(&jsondata)
	if err != nil {
		service.ReturnJson(c, 500, err.Error(), gin.H{})
		return
	}
	type ReqCheckinGetcheckinschedulistStruct struct {
		Starttime  int      `json:"starttime"`
		Endtime    int      `json:"endtime"`
		Useridlist []string `json:"useridlist"`
	}

	//转换数据类型
	s := []string{}
	for _, val := range jsondata["userlist"].([]interface{}) {
		s = append(s, val.(string))
	}

	ReqReqCheckinGetcheckinschedulistdata := ReqCheckinGetcheckinschedulistStruct{
		Starttime:  cast.ToInt(jsondata["starttime"]),
		Endtime:    cast.ToInt(jsondata["endtime"]),
		Useridlist: s,
	}

	b, err := json.Marshal(&ReqReqCheckinGetcheckinschedulistdata)
	if err != nil {
		service.ReturnJson(c, 500, err.Error(), gin.H{})
		return
	}

	fmt.Println(string(b))

	datas, err := utils.CheckinGetcheckinschedulist(b)
	service.ReturnJson(c, 200, "获取成功", gin.H{
		"info": datas.ScheduleList,
	})
}

func (wxwork Wxwork) CheckinGetcheckinoption(c *gin.Context) {
	var err error
	//数据获取
	jsondata := make(map[string]interface{})
	err = c.BindJSON(&jsondata)
	if err != nil {
		service.ReturnJson(c, 500, err.Error(), gin.H{})
		return
	}
	type ReqCheckinGetcheckinoptionStruct struct {
		Datetime   int      `json:"datetime"`
		Useridlist []string `json:"useridlist"`
	}

	//转换数据类型
	s := []string{}
	for _, val := range jsondata["userlist"].([]interface{}) {
		s = append(s, val.(string))
	}

	ReqCheckinGetcheckinoptiondata := ReqCheckinGetcheckinoptionStruct{
		Datetime:   cast.ToInt(jsondata["datetime"]),
		Useridlist: s,
	}

	b, err := json.Marshal(&ReqCheckinGetcheckinoptiondata)
	if err != nil {
		service.ReturnJson(c, 500, err.Error(), gin.H{})
		return
	}

	fmt.Println(string(b))

	datas, err := utils.CheckinGetcheckinoption(b)
	service.ReturnJson(c, 200, "获取成功", gin.H{
		"info": datas.Info,
	})
}
